#include "dialog_sfatigue.h"
#include "ui_dialog_sfatigue.h"
#include <QRegExpValidator>
#include <QCheckBox>
#include <QRect>

// 疲劳驾驶
Dialog_Sfatigue::Dialog_Sfatigue(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Dialog_Sfatigue)
{
    ui->setupUi(this);

    SERIALPORTCONFIG_INSTANCE->getSerialPortInfo(m_vecSerialInfo);
    SERIALPORTCONFIG_INSTANCE->getSettingInfo(m_settingInfo);

    // 正则表达式
    QRegExp regxName("^[0-9]+$");
    QValidator *validator = new QRegExpValidator(regxName);

    PSfatigueInfo info = m_settingInfo.m_psfatigueInfo;
    // 疲劳报警
    ui->fatigueEdit->setValidator(validator);
    ui->fatigueEdit->setText(QString::number(info.fatigue));
    // 左顾右盼报警
    ui->gazeRoundEdit->setValidator(validator);
    ui->gazeRoundEdit->setText(QString::number(info.gaze_round));
    // 无人像报警
    ui->noPortraitEdit->setValidator(validator);
    ui->noPortraitEdit->setText(QString::number(info.no_portrait));
    // 超速报警
    ui->speedingEdit->setValidator(validator);
    ui->speedingEdit->setText(QString::number(info.speeding));

    // 添加checkbox
    m_btnGroup = new QButtonGroup(this);
    m_btnGroup->setExclusive(false);  // 多选模式

    QCheckBox* checkbox1 = new QCheckBox(QStringLiteral("疲劳提醒"), this);
    checkbox1->setGeometry(QRect(190, 270, 120, 16));
    m_btnGroup->addButton(checkbox1, 0);
    if(info.warnType.contains("5101")){
        checkbox1->setCheckState(Qt::CheckState::Checked);
        m_checkBox.insert(0, "5101");
    }

    QCheckBox* checkbox2 = new QCheckBox(QStringLiteral("疲劳报警"), this);
    checkbox2->setGeometry(QRect(310, 270, 120, 16));
    m_btnGroup->addButton(checkbox2, 1);
    if(info.warnType.contains("5102")){
        checkbox2->setCheckState(Qt::CheckState::Checked);
        m_checkBox.insert(1, "5102");
    }

    QCheckBox* checkbox3 = new QCheckBox(QStringLiteral("疲劳警告"), this);
    checkbox3->setGeometry(QRect(430, 270, 120, 16));
    m_btnGroup->addButton(checkbox3, 2);
    if(info.warnType.contains("5103")){
        checkbox3->setCheckState(Qt::CheckState::Checked);
        m_checkBox.insert(2, "5103");
    }

    QCheckBox* checkbox4 = new QCheckBox(QStringLiteral("左顾右盼报警"), this);
    checkbox4->setGeometry(QRect(190, 300, 120, 16));
    m_btnGroup->addButton(checkbox4, 3);
    if(info.warnType.contains("5200")){
        checkbox4->setCheckState(Qt::CheckState::Checked);
        m_checkBox.insert(3, "5200");
    }

    QCheckBox* checkbox5 = new QCheckBox(QStringLiteral("无人像报警"), this);
    checkbox5->setGeometry(QRect(310, 300, 120, 16));
    m_btnGroup->addButton(checkbox5, 4);
    if(info.warnType.contains("5300")){
        checkbox5->setCheckState(Qt::CheckState::Checked);
        m_checkBox.insert(4, "5300");
    }

    // 关联信号
    connect(m_btnGroup, SIGNAL(buttonClicked(int)), this, SLOT(btnGroupCallBack(int)));

    // 关闭信号槽
    connect(ui->closeBtn,SIGNAL(clicked()),this,SLOT(close()));

    // 串口号
    strName = m_vecSerialInfo[0].name;
    if(SERIALPORTCTRL_INSTANCE->isOpened(strName)){
        ui->serialportBtn->setText("关闭串口");
    }



}

Dialog_Sfatigue::~Dialog_Sfatigue()
{
    delete ui;
}

// 操作串口
void Dialog_Sfatigue::on_serialportBtn_clicked()
{
    // 打开串口
    if(ui->serialportBtn->text() == "打开串口"){
        // 疲劳报警
        QString fatigueEdit = ui->fatigueEdit->text();
        // 左顾右盼报警
        QString gazeRoundEdit = ui->gazeRoundEdit->text();
        // 无人像报警
        QString noPortraitEdit = ui->noPortraitEdit->text();
        // 超速报警
        QString speedingEdit = ui->speedingEdit->text();
        // 报警类型
        QList<QString> checkBoxList = m_checkBox.values();
        QString warnType = checkBoxList.join(",");

        PSfatigueInfo info;
        info.fatigue = fatigueEdit.toInt();
        info.gaze_round = gazeRoundEdit.toInt();
        info.no_portrait = noPortraitEdit.toInt();
        info.speeding =	speedingEdit.toInt();
        info.warnType = warnType;

        PSettingInfo settingInfo;
        settingInfo.m_psfatigueInfo = info;
        // 保存数据
        SERIALPORTCONFIG_INSTANCE->setSettingInfo(settingInfo,strName);
        SERIALPORTCONFIG_INSTANCE->getSettingInfo(m_settingInfo);

        // 打开串口
        SERIALPORTCTRL_INSTANCE->open_port(strName);
        ui->serialportBtn->setText("关闭串口");
    }else{
        // 关闭串口
        SERIALPORTCTRL_INSTANCE->close_port(strName);
        ui->serialportBtn->setText("打开串口");
    }
}

// 选择框响应
void Dialog_Sfatigue::btnGroupCallBack(int id)
{
    if(m_btnGroup->button(id)->isChecked()){
        // 选择
        switch (id) {
            case 0:
                m_checkBox.insert(id, "5101");
            break;
            case 1:
                m_checkBox.insert(id, "5102");
            break;
            case 2:
                m_checkBox.insert(id, "5103");
            break;
            case 3:
                m_checkBox.insert(id, "5200");
            break;
            case 4:
                m_checkBox.insert(id, "5300");
            break;
        }
    }else{
        m_checkBox.remove(id);
    }
}
